﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

namespace School.UserCentents
{
    public partial class AddRoom : UserCheckPower
    {
        #region 传递参数
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            get
            {
                if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["id"].ToString());
            }
        }
        #endregion
        BLL.School Sch = new BLL.School();
        BLL.Users Users = new BLL.Users();
        BLL.Room Room = new BLL.Room();
        protected void Page_Load(object sender, EventArgs e)
        {
            Panel p1 = (Panel)Master.FindControl("Panel1");
            Panel p2 = (Panel)Master.FindControl("Panel2");
            Panel p3 = (Panel)Master.FindControl("Panel3");
            Panel p4 = (Panel)Master.FindControl("Panel4");
            p1.CssClass = "sldmenuoff";
            p2.CssClass = "sldmenuon0";
            p3.CssClass = "sld1";
            p4.CssClass = "sld0";
            p3.Attributes.Add("style", "display: none");
            p4.Attributes.Add("style", "");
            if (!IsPostBack)
            {
                if (new BLL.Users().GetModel(LoginUserId).UserTypeID <= 2)
                {
                    xuexiao.Visible = true;
                    DropDownList1.DataSource = Sch.GetList("CreateUserID=" + LoginUserId);
                    DropDownList1.DataValueField = "ID";
                    DropDownList1.DataTextField = "Name";
                    DropDownList1.DataBind();
                    DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));
                }

                if (ID > 0)
                {
                    Model.Room c = Room.GetModel(ID);
                    if (c != null)
                    {
                        if (new BLL.Users().GetModel(LoginUserId).UserTypeID <= 2)
                        {
                            DropDownList1.SelectedValue = c.SchoolID.ToString();
                        }
                        txtName.Text = c.Name;
                    }
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ID > 0)
            {
                Model.Room c = Room.GetModel(ID);
                c.Name = txtName.Text;
                if (xuexiao.Visible == true)
                {
                    if (DropDownList1.SelectedValue == "0")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('请选择学校！');</script>", false);
                        return;
                    }
                    c.SchoolID = int.Parse(DropDownList1.SelectedValue);
                }

                Room.Update(c);
                ScriptHelper.AlertRedirect("修改成功！", "AddRoom.aspx?id=" + ID);
            }
            else
            {
                Model.Room c = new Model.Room();
                c.Name = txtName.Text;
                if (xuexiao.Visible == true)
                {
                    if (DropDownList1.SelectedValue == "0")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('请选择学校！');</script>", false);
                        return;
                    }
                    c.SchoolID = int.Parse(DropDownList1.SelectedValue);
                    c.Remark1 = LoginUserId.ToString();
                }
                else
                {
                    c.SchoolID = Users.GetModel(LoginUserId).SchoolID;
                    c.Remark1 = Sch.GetModel(int.Parse(c.SchoolID.ToString())).CreateUserID.ToString();
                }
                c.CreateUserID = LoginUserId;
                c.CreateTime = DateTime.Now;

                Room.Add(c);
                ScriptHelper.AlertRedirect("增加成功！", "Room.aspx");
            }
        }
    }
}